<?php
require_once('../model/m_schedule_registry.php');
require_once('../model/m_users.php');

class staffing_control{

	public $usersData = array();
	public $schedulesData = array();
	public $month;
	public $year;
	public $activeDay;
	public $currentDaySchedule;
	public $scheduleStatusData = array();

	public function actionView(){
		include_once("../views/v_session.php");
		include_once("../views/v_header_principal_staffing_control_ajax.html");
		include_once("../views/v_staffing_control.html");
		include_once("../views/v_footer_principal.html");
	}	

	public function actionViewCalendarSchedules($month, $year){
		$this->month = $month;
		$this->year = $year;
		$this->usersData = getUsernames(); //Get list of users	
		$this->schedulesData = getMonthSchedules($month,$year,$this->usersData); //Get schedules of users for a specific month
		include_once("../views/_v_calendar.php");
	}	

	public function actionViewEditDay($username,$year,$month,$day){
		$this->currentUser = $username;
		$this->currentDay= $day;
		$this->currentMonth= $month;
		$this->currentYear= $year;
		$this->currentDaySchedule = getUserScheduleStatus($username,$year,$month,$day); //Get schedule for specific user
		$this->scheduleStatusData = loadScheduleStatus();
		include_once("../views/_v_staffing_control_user_edit.html");
	}

	public function actionUpdateSchedule($username,$calendarDay,$statusId){
		$this->currentUser = $username;	
		
		if(saveScheduleUser($username,$calendarDay,$statusId)):?>
			<br><br><span class='top-bar'><h1>Changes Saved Successfully!!!</h1></span>
			<image src="../images/check-icon.gif" onload="openCalendarView();">

			
		<?php
			exit;
		else:
			echo "<h1>THERE WAS A PROBLEM WHEN TRYING TO SAVE THE CHANGES!!!</h1>";
			exit;
		endif;
	
	}
	
	public function actionRemoveSchedule($username,$calendarDay){
		
		if(deleteScheduleUser($username,$calendarDay)):?>
			<br><br><span class='top-bar'><h1>Changes Saved Successfully!!!</h1></span>
			<image src="../images/check-icon.gif" onload="openCalendarView();">

			
		<?php
			exit;
		else:
			echo "<h1>THERE WAS A PROBLEM WHEN TRYING TO SAVE THE CHANGES!!!</h1>";
			exit;
		endif;
	
	}
}

$instancia= new staffing_control;

if(isset($_GET['view'])){
	if($_GET['view']=="list")	
		$instancia->actionView();
}

if(isset($_GET['viewCalendar'])){
	if($_GET['viewCalendar']=="true")
		$instancia->actionViewCalendarSchedules($_GET['month'],$_GET['year']);
}

if(isset($_GET['editDay'])){
	if($_GET['editDay']=="true")
		$instancia->actionViewEditDay($_GET['user'],$_GET['year'],$_GET['month'],$_GET['day']);
}


if(isset($_GET['update'])){
	if($_GET['update']=="false"){
		echo "<br><br><span class='top-bar'><h1>Action Cancelled </h1></span>";
		exit;
	} else {
		if($_GET['status']=='0')
			$instancia->actionRemoveSchedule($_GET['update'],$_GET['calendarDay'],$_GET['status']);
		else
			$instancia->actionUpdateSchedule($_GET['update'],$_GET['calendarDay'],$_GET['status']);
	}
}
?>